<?php

/*
 * ************************************************* 
 * Created on :2012-3-20 8:28:12
 * Encoding   :UTF-8
 * Description:
 * 
 * @Author @lqs <soso2501@gmail.com>
 * @WAP_WEIBO (C)1996-2099 SINA Inc. 
 * ************************************************
 */

class main extends Controller {

    public function init() {
        $this->selfc = "main";
        $this->tpl->assign("title", "河北工业大学校友会");
    }

    public function index() {
        //校友快讯
        $xykx = News::bytype(array("校友会快讯", "人物动态", "领导关怀"), 0, 6);
        Toolfunc::addnewslink($xykx);
        $this->tpl->assign("xykx", $xykx);

        //讲堂动态
        $jtdt = News::bytype(array("讲堂动态"), 0, 6);
        Toolfunc::addnewslink($jtdt);
        $this->tpl->assign("jtdt", $jtdt);

        //母校新闻
        $mxxw = News::bytype(array("河工新闻"), 0, 6);
        Toolfunc::addnewslink($mxxw);
        $this->tpl->assign("mxxw", $mxxw);

        //最新服务
        $zxfw = News::bytype(array("最新服务"), 0, 6);
        Toolfunc::addnewslink($zxfw);
        $this->tpl->assign("zxfw", $zxfw);

        //分会新闻
        $fwxw = News::bytype(array("分会新闻"), 0, 6);
        Toolfunc::addnewslink($fwxw);
        $this->tpl->assign("fhxw", $fwxw);

        //校友捐赠
        $xyjz = News::bytype(array("捐赠事迹", "年度捐赠"), 0, 6);
        Toolfunc::addnewslink($xyjz);
        $this->tpl->assign("xyjz", $xyjz);

        $this->tpl->display("main/index.html");
    }

    public function info() {
        if ($this->user->state != 0) {
            header("location:/main/login");
            die();
        }
        if (!empty($_SESSION['msg'])) {
            $this->tpl->assign("msg", $_SESSION['msg']);
            unset($_SESSION['msg']);
        }
        $this->tpl->display("main/info.html");
    }

    public function modinfo() {
        if (!empty($_REQUEST)) {
            switch ($_REQUEST['tag']) {
                case "password":
                    if ($_REQUEST['new'] != $_REQUEST['agn']) {
                        $_SESSION['msg'] = "两次密码不一致";
                    } else {
                        $r = $this->user->changepwd(md5($_REQUEST['old']), md5($_REQUEST['agn']));
                        $_SESSION['msg'] = $r;
                    }
                    break;
                default:
                    $data = array($_REQUEST['tag'] => $_REQUEST['newv']);
                    var_dump($data);
                    if ($this->user->update($data))
                        $_SESSION['msg'] = "修改成功";
                    else
                        $_SESSION['msg'] = "修改失败";
            }
        }
        header("location:/main/info");
    }

    public function login() {
        if (!empty($_POST)) {
            $r = $this->user->checklogin(trim($_REQUEST['username']), md5($_REQUEST['password']));
            //var_dump($r);
            if ($r == 0)
                $msg = '密码或用户名错误';
            else if ($r == 2)
                $msg = "你的用户还没有通过审核，请耐心等待";
            else {
                header("location:/main/info");
                return;
            }
            $this->tpl->assign("msg", $msg);
        }
        $this->tpl->display("main/login.html");
    }

    public function register() {
        if (!empty($_POST)) {
            $data = $_POST;
            if ($data['agn'] != $data['password']) {
                $msg = "两次密码不一样";
            } else {
                unset($data['agn']);
                $data['password'] = md5($data['password']);
                $r = $this->user->register($data);
                if ($r == -1) {
                    $msg = "用户名已经被注册";
                } else if ($r == -2) {
                    $msg = "注册失败，请将信息填完整";
                } else {
                    $msg = "注册成功，请等待审核";
                }
            }
            $this->tpl->assign("js_set", json_encode($_POST));
            $this->tpl->assign("msg", $msg);
        }
        $this->tpl->display("main/register.html");
    }

    public function news() {
        $this->tpl->display("main/news.html");
    }

    public function s() {
        $this->tpl->display("main/single.html");
    }

    public function checklogin() {
        var_dump($_COOKIE);
        $r = $this->user->checklogin('so', md5('abc'), true);
        var_dump($r);
    }

    public function role() {
        DB::Debug();
        $r = $this->user->pass(3);
        var_dump($r);
    }

    public function getnews() {
        DB::Debug();
        $r = News::bytype("河工新闻");
        var_dump($r);
    }

    public function addnews() {
        DB::Debug();
        $r = News::add("河工新闻", "so同学来了", "他真的来了。哈哈");
        var_dump($r);
    }

    public function modnews() {
        DB::Debug();
        $r = News::modify(2, array("title" => "是吗？？"));
        var_dump($r);
    }

}

/* End of file main */


